import { getSort, getMutipSort } from '/@/views/chat/utils/functions.js'
import { defineStore } from 'pinia';
import { ChatTalk } from '../interface';
import { formateTalkItem } from '/@/views/chat/utils/talk.js'
import { useChatApi } from '/@/api/chat/index'
const chatApi = useChatApi()
/**
 * chat talk 模块数据
 */
export const useChatTalk = defineStore('chatTalk', {
    state: (): ChatTalk => ({
        loadStatus: 1, // 加载状态[1:未加载;2:加载中;3:加载完成;4:加载失败;]
        items: [], // 会话列表
        unreadMessage: { // 最后一条消息
            num: 0,
            nickname: '未知',
            content: '...'
        }
    }),
    getters: {
        topItems: state => {
            return state.items.filter(item => item.is_top == 1)
        },
        talkItems: state => {
            return state.items.sort(
                getMutipSort([getSort((a:any, b:any) => a.updated_at > b.updated_at)])
            )
        },
        unreadNum: state => {
            return state.items.reduce((total, item) => {
                return total + parseInt(item.unread_num)
            }, 0)
        },
        talkNum: state => state.items.length
    },
    actions: {
        SET_LOAD_STATUS(resource: any) {
            this.loadStatus = resource
        },
        // 设置对话列表
        SET_TALK_ITEMS(resource: any) {
            this.items = resource.items
        },
        // 更新对话节点
        UPDATE_TALK_ITEM(resource: any) {
            for (const iterator of this.items) {
                if (iterator.index_name === resource.index_name) {
                    Object.assign(iterator, resource)
                    break
                }
            }
        },
    
        // 新增对话节点
        PUSH_TALK_ITEM(resource: any) {
            this.items.push(resource)
        },
    
        // 移除对话节点
        REMOVE_TALK_ITEM(index_name:any) {
            var i:any;
            for (i in this.items) {
                if (this.items[i].index_name === index_name) {
                    this.items.splice(i, 1)
                    break
                }
            }
        },
    
        // 更新对话消息
        UPDATE_TALK_MESSAGE(resource:any) {
            for (const iterator of this.items) {
                if (iterator.index_name !== resource.index_name) {
                    continue
                }
                iterator.unread_num++
                iterator.msg_text = resource.msg_text
                iterator.updated_at = resource.updated_at
                break
            }
        },
    
        SET_TLAK_UNREAD_MESSAGE(resource:any) {
            this.unreadMessage.num++
            this.unreadMessage.nickname = resource.nickname
            this.unreadMessage.content = resource.content
        },
    
        // 清除最后一条未读消息
        CLEAR_TLAK_UNREAD_MESSAGE() {
            this.unreadMessage = {
                num: 0,
                nickname: '未知',
                content: '...',
            }
        },
        LOAD_TALK_ITEMS() {
            this.loadStatus = 2
            chatApi.ServeGetTalkList().then((res: any) => {
                const data: any = res.data.items
                this.items = data.map((item: any) => formateTalkItem(item))
                this.loadStatus = 3
            }).catch(() => {
                this.loadStatus = 4
            })
        }
    }
})
